﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TugasAkhir.Helpers;
using TugasAkhir.Models;

namespace TugasAkhir
{
    public partial class Dummy : System.Web.UI.Page
    {
        private int[,] data;
        private int totalPopulasi;
        private int totalPertanyaan;

        private double[] nilaiAkhir;
        private int[] kehadiran;
        private double[,] data2;

        private DataSet hasilQuery;
        private DataSet hasilQuery2;

        private List<int> pengecualian;

        protected void Page_Load(object sender, EventArgs e)
        {
            TabelR();
            FillData();
            HitungKK();
            HitungKR();
            HitungSkor();
            HitungFuzzy();
        }

        private void TabelR()
        {
            RTabel.Instance.SetPath(System.IO.Path.GetFullPath(Server.MapPath("~/data.txt")));
            this.R.Text = RTabel.Instance[34].ToString();
        }

        private void HitungFuzzy()
        {
            var A = new List<double>();
            var AKehadiran = new List<double>();

            var fuzzyCalc = new FuzzyCalculation(this.totalPopulasi, this.totalPertanyaan);
            fuzzyCalc.InitKehadiran(this.kehadiran);
            fuzzyCalc.InitNilai(this.nilaiAkhir);
            fuzzyCalc.InitA(this.data2);
            fuzzyCalc.Calculate(out A, out AKehadiran);

            for (int i = 0; i < A.Count; i++)
            {
                this.fuzzy.Text += A[i] + "<br/>" + AKehadiran[i] + "<br/><br/>";
            }
        }

        private void HitungSkor()
        {
            var theSkor = new Skor(totalPopulasi, totalPertanyaan, data, 4);
            var hasil = theSkor.HitungSkor();

            foreach (var item in hasil)
            {
                this.skor.Text += item + "<br/>";
            }
        }

        private void HitungKR()
        {
            var koefisien = new KoefisienReliabilitas(totalPopulasi, totalPertanyaan, data);
            this.KR.Text = koefisien.HitungKoefisienReliabilitas(pengecualian) + "<br />";
        }

        private void FillData()
        {
            PerhitunganModel.GetJumlahPeserta("MK04", "2010-2011", "GASAL", out this.totalPopulasi);
            PerhitunganModel.GetJumlahPertanyaan("MK04", "2010-2011", "GASAL", out this.totalPertanyaan);

            this.data = new int[this.totalPopulasi, this.totalPertanyaan];
            this.data2 = new double[this.totalPopulasi, this.totalPertanyaan];

            if (PerhitunganModel.GetTabelNilai("MK04", "2010-2011", "GASAL", out hasilQuery))
            {
                for (int i = 0; i < hasilQuery.Tables[0].Rows.Count; i++)
                {
                    var item = hasilQuery.Tables[0].Rows;
                    string[] num = item[i][4].ToString().Split(new char[] { ',' });

                    for (int j = 0; j < num.Length; j++)
                    {
                        this.data[i, j] = int.Parse(num[j]);
                        this.data2[i, j] = double.Parse(num[j]);
                    }
                }
            }

            this.nilaiAkhir = new double[this.totalPopulasi];
            this.kehadiran = new int[this.totalPopulasi];

            if (PerhitunganModel.GetNilaiAkhir("MK04", "2010-2011", "GASAL", out hasilQuery2))
            {
                for (int i = 0; i < hasilQuery2.Tables[0].Rows.Count; i++)
                {
                    var item = hasilQuery2.Tables[0].Rows[i];
                    this.kehadiran[i] = Convert.ToInt32(item[0].ToString());
                    this.nilaiAkhir[i] = Convert.ToDouble(item[1].ToString());
                }
            }

            this.pengecualian = new List<int>();
        }

        private void HitungKK()
        {
            var koefisien = new KoefisienKorelasi[this.totalPertanyaan];
            var Y = new List<int>();
            var Xs = new List<int>[this.totalPertanyaan];

            // init masing-masing X
            for (int i = 0; i < this.totalPertanyaan; i++)
            {
                Xs[i] = new List<int>();
            }

            // dapatkan Y
            for (int i = 0; i < this.totalPopulasi; i++)
            {
                int y = 0;
                for (int j = 0; j < this.totalPertanyaan; j++)
                {
                    y += data[i, j];
                }

                Y.Add(y);
            }

            for (int i = 0; i < this.totalPertanyaan; i++)
            {
                koefisien[i] = new KoefisienKorelasi();
                koefisien[i].UkuranPopulasi = this.totalPopulasi;

                for (int j = 0; j < totalPopulasi; j++)
                {
                    Xs[i].Add(this.data[j, i]);
                }

                foreach (var item in Xs[i])
                    koefisien[i].TambahSkorItem(item);

                foreach (var item in Y)
                    koefisien[i].TambahSkorTotalItem(item);

                double hasil = koefisien[i].HitungKoefisienKorelasi();
                if (hasil < RTabel.Instance[this.totalPopulasi - 2])
                {
                    this.pengecualian.Add(i);
                }

                this.KK.Text += hasil + "<br />";
            }
        }
        //{
        //    var dataTabel = new System.Data.DataSet();
        //    var Xs = new List<int>();
        //    var Ys = new List<int>();

        //    var Xss = new Dictionary<int, List<int>>();

        //    var koefisien = new Helpers.KoefisienKorelasi();
        //    int n;

        //    if (PerhitunganModel.GetJumlahPeserta("MK04", "2010-2011", "GASAL", out n))
        //    {
        //        koefisien.UkuranPopulasi = n;
        //        if (PerhitunganModel.GetTabelNilai(koefisien.UkuranPopulasi, "MK04", "GASAL", "2010-2011", out dataTabel))
        //        {
        //            for (int i = 0; i < dataTabel.Tables[0].Rows.Count; i++)
        //            {
        //                var item = dataTabel.Tables[0].Rows;
        //                string[] num = item[i][4].ToString().Split(new char[] { ',' });

        //                int y = 0;

        //                for (int j = 0; j < num.Length; j++)
        //                {
        //                    y += int.Parse(num[j]);
        //                }

        //                Xs.Add(int.Parse(num[0]));

        //                Ys.Add(y);
        //            }

        //        }

        //    }

        //    foreach (var item in Xs)
        //        koefisien.TambahSkorItem(item);

        //    foreach (var item in Ys)
        //        koefisien.TambahSkorTotalItem(item);

        //    System.Diagnostics.Debug.WriteLine(koefisien.HitungKoefisienKorelasi());
        //}
    }
}